package burp.models;

import org.json.JSONObject;

public class Vulnerability {

    private String id;
    private String type;
    private String title;
    private Double cvssScore;
    private String description;
    private String bulletinFamily;

    private String exploit = "";
    private String flatDescription = "";

    public Vulnerability(JSONObject vulnersBulletin) {
        this.id = vulnersBulletin.getString("id");
        this.type = vulnersBulletin.getString("type");
        this.title = vulnersBulletin.getString("title");
        this.description = vulnersBulletin.getString("description");
        this.bulletinFamily = vulnersBulletin.getString("bulletinFamily");

        if (vulnersBulletin.has("flatDescription")) {
            this.flatDescription = vulnersBulletin.getString("flatDescription");
        }
        if (vulnersBulletin.has("sourceData") && bulletinFamily.equals("exploit")) {
            this.exploit = vulnersBulletin.getString("sourceData");
        }
        this.cvssScore = vulnersBulletin.getJSONObject("cvss").getDouble("score");
    }

    public String getBulletinFamily() {
        return bulletinFamily;
    }

    public String getItemLink() {
        return getItemLink(id);
    }

    public String getExploitLink() {
        return "".equals(exploit) ? "" : getItemLink("Exploit");
    }

    public String getItemLink(String value) {
        return String.format("<a href=\"https://vulners.com/%s/%s\" target=\"_blank\">%s</a>", type, id, value);
    }

    public String getItemDescription() {
        return !flatDescription.equals("")
                ? flatDescription
                : description.length() > 256
                ? description.substring(0, 256) + "..."
                : description;
    }

    public String getItemCvssScore() {
        return cvssScore.intValue() > 0 ? String.format("<b color=\"red\">%s<b/>", cvssScore) : "";
    }

    public String getFlatDescription() {
        return flatDescription;
    }

    public String getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public double getCvssScore() {
        return cvssScore;
    }

    public String getType() {
        return type;
    }

    public String getDescription() {
        return description;
    }
}
